#~ Copyright 2008 Brian Simons
#~ 
#~ This file is part of usetv.
#~ 
#~ usetv is free software: you can redistribute it and/or modify
#~ it under the terms of the GNU General Public License as published by
#~ the Free Software Foundation, either version 3 of the License, or
#~ (at your option) any later version.
#~ 
#~ usetv is distributed in the hope that it will be useful,
#~ but WITHOUT ANY WARRANTY; without even the implied warranty of
#~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#~ GNU General Public License for more details.
#~ 
#~ You should have received a copy of the GNU General Public License
#~ along with this program.  If not, see <http://www.gnu.org/licenses/>
"""Unit test for usetv.py"""
import os
from usetv import UseTV
import unittest
import feedparser

class GrabNZBtest(unittest.TestCase):
    def test_grabNZB_saves_file(self):
        """grab_nzb should create a file in ./NZB on disk"""
        url = 'http://www.google.com'
        file_name = 'grab_nzb_test.html'
        path = os.path.join(os.getcwd(), 'NZB', file_name)
        t = UseTV()
        if os.path.exists(path):
                os.unlink(path)
        t.grab_nzb(url, file_name)
        try:
            self.failUnless(os.path.exists(path))
        finally:
            os.unlink(path)
        
    def test_url_doesnt_exist(self):
        """an exception should be raised if URL is bad"""
        t = UseTV()
        self.assertRaises(
            IOError, 
            t.grab_nzb,
            'http://www.sdsdfgsfjskhkjhruihiusdfsf.com', 
            'grab_nzb_test.html'
            )
            
    def test_dir_cant_be_created(self):
        """
        an exception should be raised if the directory couldn't be created
        """
        t=UseTV()
        self.assertRaises(
            IOError,
            t.grab_nzb,
            'http://www.google.com',
            '/grab_nzb_test.html'
            )
            
    #~ def test_file_already_downloaded(self):
        #~ """duplicate files shouldn't be downloaded"""
        #~ url = 'http://www.google.com'
        #~ file_name = 'duptest.html'
        #~ path = os.path.join(os.getcwd(), 'NZB', file_name)
        #~ t=UseTV()
        #~ t.grab_nzb(url, file_name)
        #~ os.utime(path, (0.0, 0.0))
        #~ time1 = os.stat(path).st_mtime
        #~ t.grab_nzb(url, file_name)
        #~ time2 = os.stat(path).st_mtime
        #~ try:
            #~ self.assertEqual(time1, time2)
        #~ finally:
            #~ os.unlink(path)
            
class GetLatestEpisodeNumberTVNZBtest(unittest.TestCase):
    t = UseTV()
    episode = t.get_latest_episode_number_tvnzb("eureka")
    
    def test_returns_dict(self):
        """get_latest_episode_number_tvnzb should return a dict"""
        self.assertEqual(
            type(dict()),
            type(self.episode)
            )
            
    def test_episode_is_integer(self):
        """episode should be at least 1 integer, all greater than 0"""
        self.assertNotEqual(len(self.episode), 0)
        for i in self.episode:
            self.assertEqual(type(int()), type(self.episode[i]))
            self.assertNotEqual(i, 0)
            
    def test_feeds_has_data(self):
        """UseTV.feeds should have data now"""
        self.assertNotEqual(len(self.t.feeds), 0)
        
    def test_feeds_item_is_rss_object(self):
        """items in UseTV.feeds should be rss objects"""
        self.assertEqual(type(self.t.feeds['tvnzb']), type(feedparser.FeedParserDict()))
            

class GetLatestEpisodeNumberNzbindextest(unittest.TestCase):
    t = UseTV()
    episode = t.get_latest_episode_nzbindex("eureka")
    
    def test_returns_dict(self):
        """get_latest_episode_number_nzbindex should return a dict"""
        self.assertEqual(
            type(feedparser.FeedparserDict()),
            type(self.episode)
            )
            
    def test_episode_is_integer(self):
        """episode should be at least 1 integer, all greater than 0"""
        self.assertNotEqual(len(self.episode), 0)
        for i in self.episode:
            self.assertEqual(type(int()), type(self.episode[i]))
            self.assertNotEqual(i, 0)
            
    def test_feeds_has_data(self):
        """UseTV.feeds should have data now"""
        self.assertNotEqual(len(self.t.feeds), 0)
        
    def test_feeds_item_is_rss_object(self):
        """items in UseTV.feeds should be rss objects"""
        self.assertEqual(
            type(self.t.feeds['nzbindex']),
            type(feedparser.FeedParserDict())
            )
if __name__ == "__main__":
    unittest.main()
